# -*- coding:utf8 -*-
import numpy as np
import matplotlib.pylab as plt

def get_fcl_from_txt_file(visualization=False):
    print "getting forward_comment_like from txt"
    user_id = []
    forward = []
    comment = []
    like =[]
    forward_comment_like = []
    for line in open('C:\\Users\\wuxiaomin\\Desktop\\data\\SinaWeiBo\\weibo_train_data.txt','r'):
        list_line = line.split()
        user_id.append(list_line[0])
        forward.append(list_line[3])
        comment.append(list_line[4])
        like.append(list_line[5])
        forward_comment_like.append(list_line[3:6])

    forward = np.asarray(forward, dtype='float32')
    comment = np.asarray(comment, dtype='float32')
    like = np.asarray(like, dtype='float32')
    forward_comment_like = np.asarray(forward_comment_like, dtype='float32')

    if visualization == True:
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        ax.scatter(forward, comment, like, c='b', marker=".")
        ax.set_xlabel('forward')
        ax.set_ylabel('comment')
        ax.set_zlabel('like')
        plt.show()
    return user_id,forward_comment_like

def get_all_zero_users_index():
    user_id,forward_comment_like = get_fcl_from_txt_file(visualization=False)
    all_zero_users_index = []
    print "getting all_zero users index"
    for i in range(len(forward_comment_like)):
        if forward_comment_like[i][0]<1 and forward_comment_like[i][1]<1 and forward_comment_like[i][2]<1:
            all_zero_users_index.append(i)
    print "all_zero items:"+str(len(all_zero_users_index))
    return user_id,all_zero_users_index

def get_all_zero_users_ID_no_repetition(save_to_txt=False):
    user_id,all_zero_users_index = get_all_zero_users_index()
    print "getting all_zero users ID"
    all_zero_users_ID = []
    for i in range(len(all_zero_users_index)):
        all_zero_users_ID.append(user_id[i])
    all_zero_users_ID_no_repetition = set(all_zero_users_ID)
    all_zero_users_ID_no_repetition = list(all_zero_users_ID_no_repetition)
    print "all_zero_users_ID_no_repetition:" + str(len(all_zero_users_ID_no_repetition))
    if save_to_txt == True:
        print "saving all_zero_users_ID_no_repetition to txt"
        f = open("C:\\Users\\wuxiaomin\\Desktop\\data\\SinaWeiBo\\all_zero_users_ID_no_repetition.txt","w")
        for j in range(len(all_zero_users_ID_no_repetition)):
            f.write(all_zero_users_ID_no_repetition[j]+"\n")
    return all_zero_users_ID_no_repetition

def generate_no_all_zero_train_txt(save_to_txt=False):
    print "generatting no_all_zero train txt"
    no_all_zero_users_train_txt = []
    all_lines = []
    index = 0
    for line in open('C:\\Users\\wuxiaomin\\Desktop\\data\\SinaWeiBo\\weibo_train_data.txt','r'):
        list_line = line.split()
        all_lines.append(list_line)
        forward_comment_like = np.asarray(list_line[3:6], dtype='float32')
        if forward_comment_like[0]>=1 or forward_comment_like[1]>=1 or forward_comment_like[2]>=1:
            no_all_zero_users_train_txt.append(line)
        index += 1
    print "no_all_zero_users_train_txt:" + str(len(no_all_zero_users_train_txt))
    if save_to_txt == True:
        print "saving no_all_zero_users_train to txt"
        f = open("C:\\Users\\wuxiaomin\\Desktop\\data\\SinaWeiBo\\no_all_zero_users_train.txt","w")
        for j in range(len(no_all_zero_users_train_txt)):
            f.write(no_all_zero_users_train_txt[j])
    return no_all_zero_users_train_txt


if __name__ == "__main__":
   a = generate_no_all_zero_train_txt(save_to_txt=True)


